package com.capgemini.cn.authority.rest.resource;

import com.capgemini.cn.authority.rest.exception.AuthRestBusinessException;
import com.capgemini.cn.authority.rest.valueobject.DataResponse;
import io.swagger.annotations.ApiParam;
import org.springframework.data.domain.Sort;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

/**
 * <p>公共查询资源接口</p>
 * Created at 2017/9/19
 *
 * @param <D> 定义返回的值对象数据类型
 * @author Andriy
 */
public interface CommonResource<D> {

    String ID_PARAM = "id";
    String PAGE_PARAM = "page";
    String SIZE_PARAM = "size";
    String ORDER_PARAM = "order";
    String DIRECTION_PARAM = "direction";

    String ID_PARAM_COMMENT = "数据ID主键值";
    String PAGE_PARAM_COMMENT = "查询的页码数，默认为0（即从第一页开始查询）";
    String SIZE_PARAM_COMMENT = "每页数据的条数，如果值为-1或0则表示查询所有的数据（即最大条数，此时分页参数失效）";
    String ORDER_PARAM_COMMENT = "排序属性名称";
    String DIRECTION_PARAM_COMMENT = "排序规则";

    @GetMapping(path = "{id}")
    DataResponse<D> findByOne(@ApiParam(value = ID_PARAM_COMMENT, name = ID_PARAM, required = true) @PathVariable(name = ID_PARAM) long id);

    @GetMapping(path = "all")
    DataResponse<List<D>> findAll(@ApiParam(value = PAGE_PARAM_COMMENT, name = PAGE_PARAM, required = true) @RequestParam(value = PAGE_PARAM) int page,
                                  @ApiParam(value = SIZE_PARAM_COMMENT, name = SIZE_PARAM, required = true) @RequestParam(value = SIZE_PARAM) int size,
                                  @ApiParam(value = ORDER_PARAM_COMMENT, name = ORDER_PARAM, required = true) @RequestParam(value = ORDER_PARAM) String order,
                                  @ApiParam(value = DIRECTION_PARAM_COMMENT, name = DIRECTION_PARAM, required = true) @RequestParam(value = DIRECTION_PARAM) Sort.Direction direction);

    @DeleteMapping(path = "remove/{id}")
    DataResponse remove(@ApiParam(value = ID_PARAM_COMMENT, name = ID_PARAM, required = true) @PathVariable(name = ID_PARAM) long id) throws AuthRestBusinessException;
}
